package com.njtcedu.microblog.website.content.repository;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.njtcedu.microblog.common.entity.Content;
import com.njtcedu.microblog.common.entity.Topic;
import com.njtcedu.microblog.common.entity.User;
import com.njtcedu.microblog.common.exception.BusinessException;
import com.njtcedu.microblog.common.repository.AbstractRepository;
import com.njtcedu.microblog.common.util.StringUtils;
import com.njtcedu.microblog.common.web.domain.PageModel;
import com.njtcedu.microblog.website.content.web.request.ContentSearchRequest;
import com.njtcedu.microblog.website.content.web.request.ContentSenderRequest;
import com.njtcedu.microblog.website.content.web.response.ContentResponse;
import com.njtcedu.microblog.website.home.web.response.CurrentUser;

@Repository

public class ContentRepository extends AbstractRepository {

	//发送微博信息
	public void SendContent(ContentSenderRequest request){
		
		User user = hibernateAccess.get(User.class, request.getUserId());
		//create the Topic
		Topic topic = new Topic();
		topic.setTopicContent(request.getSendContent());
		topic.setUser(user);
		
		hibernateAccess.persist(topic);
		hibernateAccess.currentSession().flush();
		
		Content content = new Content();
		content.setUser(user);
		content.setContentType(request.getContentType());
		content.setStatus("Y");
		content.setSendContent(request.getSendContent());
		hibernateAccess.persist(content);
	}

	public ContentResponse sendMessage(CurrentUser currentUser, String sendContent, String contentType) {
		ContentResponse response = new ContentResponse();
		User user = hibernateAccess.get(User.class, currentUser.getUserId());
		//create the topic 
		String topicContet = sendContent.substring(sendContent.indexOf("#") + 1, sendContent.lastIndexOf("#"));
		Topic topic = new Topic();
		topic.setTopicContent(topicContet);
		topic.setUser(user);
		topic.setRecommend("N");
		hibernateAccess.persist(topic);
		hibernateAccess.currentSession().flush();
		
		Content content = new Content();
		content.setUser(user);
		content.setContentType(contentType);
		content.setStatus("Y");
		content.setSendDate(getSysDate());
		String messageContent = sendContent.substring(sendContent.lastIndexOf("#") + 1);
		content.setSendContent(messageContent);
		content.setTopic(topic);
		
		hibernateAccess.persist(content);
		hibernateAccess.currentSession().flush();
		
		response.setUserId(user.getId());
		response.setUserName(user.getUserInfo().getNickName());
		response.setUserImage(user.getUserInfo().getUserImage());
		response.setTopic(topic.getTopicContent());
		response.setTopicId(topic.getId());
		response.setContentId(content.getId());
		response.setContent(content.getSendContent());
		response.setSendDate(getSysDate());
		return response;
	}
	
	public List<ContentResponse> getAllContent() {
		//User user = hibernateAccess.get(User.class, currentUser.getUserId());
		DetachedCriteria criteria = DetachedCriteria.forClass(Content.class);
		List<Content> contents = hibernateAccess.find(criteria);
		
		List<ContentResponse> contentsResponse = new ArrayList<ContentResponse>();
		
		for (Content content : contents) {
			User user = content.getUser();
			ContentResponse response = new ContentResponse();
			Topic topic = content.getTopic();
			response.setUserId(user.getId());
			response.setUserName(user.getUserInfo().getNickName());
			response.setUserImage(user.getUserInfo().getUserImage());
			response.setTopic(topic.getTopicContent());
			response.setTopicId(topic.getId());
			response.setContentId(content.getId());
			response.setContent(content.getSendContent());
			response.setSendDate(content.getSendDate());
			contentsResponse.add(response);
		}
		
		return contentsResponse;
	}
	
	public PageModel<ContentResponse> getAllContent(ContentSearchRequest request, Integer pageNo, Integer pageSize) {
		DetachedCriteria criteria = createCriteria(request);
		
		List<Content> contents = hibernateAccess.find(criteria, pageNo, pageSize);
		
		List<ContentResponse> contentsResponse = new ArrayList<ContentResponse>();
		
		for (Content content : contents) {
			User user = content.getUser();
			ContentResponse response = new ContentResponse();
			Topic topic = content.getTopic();
			response.setUserId(user.getId());
			response.setUserName(user.getUserInfo().getNickName());
			response.setUserImage(user.getUserInfo().getUserImage());
			response.setTopic(topic.getTopicContent());
			response.setTopicId(topic.getId());
			response.setContentId(content.getId());
			response.setContent(content.getSendContent());
			response.setSendDate(content.getSendDate());
			contentsResponse.add(response);
		}
		
		Long totalRecords = totalRecords(request);
		return createPageModel(pageNo, pageSize, totalRecords, contentsResponse);
	}

	private Long totalRecords(ContentSearchRequest request) {
		DetachedCriteria criteria = createCriteria(request);
		criteria.setProjection(Projections.rowCount());
		Long totalRecords = hibernateAccess.findUniqueResult(criteria);
		return totalRecords == null ? 0L :totalRecords;
	}

	private DetachedCriteria createCriteria(ContentSearchRequest request) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Content.class);
		
		criteria.add(Restrictions.eq("status", "Y"));
		
		if (request != null && request.getSendDate() != null) {
			criteria.add(Restrictions.eq("sendDate", request.getSendDate()));
		}
		
		if (request != null && StringUtils.hasText(request.getUserName())) {
			criteria = criteria.createCriteria("user").add(Restrictions.eq("account", request.getUserName()));
		}
		
		return criteria;
	}
	
	
	public List<Content> getContents() {
		return hibernateAccess.find(DetachedCriteria.forClass(Content.class).add(Restrictions.eq("status", "Y")));
	}

	public boolean removeContent(Integer contentId) {
		if (contentId == null)
			return false;
		Content content = hibernateAccess.get(Content.class, contentId);
		if (content == null) {
			throw new BusinessException("微博[" + contentId +"]不存在");
		}
		content.setStatus("N");
		hibernateAccess.update(content);
		return true;
	}
}
